package org.example.service;

import com.aliyun.oss.OSS;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.example.bean.Orderdetail;
import org.example.configs.OssConfig;
import org.example.dao.CategoryMapper;
import org.example.dao.OrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.extension.toolkit.Db.saveBatch;

@Service
@AllArgsConstructor
@Log
public class OrderdetailServiceImpl implements OrderdetailService{
    private final OrderDetailMapper orderDetailMapper;
    private final OssConfig ossConfig;
    private final OSS ossClient;

    @Override
    @Transactional // 添加事务管理
    public boolean addOrderDetail(Integer orderId, List<Orderdetail> detailList) {
        if (orderId == null || detailList == null || detailList.isEmpty()) {
            throw new IllegalArgumentException("参数不能为空");
        }

        // DTO转Entity
        List<Orderdetail> entities = detailList.stream()
                .map(dto -> {
                    Orderdetail detail = new Orderdetail();
                    detail.setOrder_id(orderId);
                    detail.setProduct_id(dto.getProduct_id());
                    detail.setCount(dto.getCount());
                    detail.setCurrent_price(dto.getCurrent_price());
                    return detail;
                })
                .collect(Collectors.toList());

        // 批量插入
        return saveBatch(entities);
    }

    @Override
    public List<Orderdetail> getOrderDetailByIds(List<Integer> ids) {

        return orderDetailMapper.selectBatchIds(ids);
    }





}
